﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Estimate.Common.Repository;
using System.Data.SqlClient;
using Estimate.Common.DBFieldTypes;
using log4net;

namespace Estimate.Common.DBHealth
{
    public class ScheduleHealth
    {
        public string _connectionString;

        public static ILog Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        private Dictionary<string, int> _dayRecordCount = new Dictionary<string, int>();

        public ScheduleHealth(string connectionString)
        {
            _connectionString = connectionString;

            List<string> spans = Enum.GetNames(typeof(ForeSpanType)).ToList<string>();
            List<string> clocks = Enum.GetNames(typeof(ForeClockType)).ToList<string>();
            List<string> sendpoints = new List<string>();
            foreach (ForeSendpointType sendpoint in new ForeSendpointType[] { ForeSendpointType.H06, ForeSendpointType.H10, ForeSendpointType.H15, ForeSendpointType.H16 })
            {
                sendpoints.Add(sendpoint.ToString());
            }

            foreach (string span in spans)
            {
                foreach( string clockSendpoint in new string[]{"H08*H06", "H08*H10","H20*H16"})
                    {
                        string key = string.Format("{0}*{1}", span,clockSendpoint);
                        _dayRecordCount[key] = 0;
                    }
                
            }

        }

        private void SetRecord(string span, string clock, string sendpoint,int count)
        {
            string key = string.Format("{0}*{1}*{2}", span, clock, sendpoint);
            if( _dayRecordCount.ContainsKey(key))
            {
             _dayRecordCount[key] += count;
            }
        }

        public void Clear()
        {
            List<string> key_list = new List<string>( _dayRecordCount.Count );
            foreach(string key in _dayRecordCount.Keys)
            {
                key_list.Add(key);
            }

            foreach (string key in key_list)
            {
                _dayRecordCount[key] = 0;
            }
        }

        public void IsHealth(DateTime date, string station)
        {
            Clear();         
            string sql = String.Format("SELECT span, clock, sendpoint, COUNT(*) as count   FROM [Estimate].[dbo].[Schedule]   where stations = '{0}' and realdate = '{1}'   group by span ,clock, sendpoint  order by span, clock, sendpoint", station ,date.ToString("yyyy-MM-dd"));
            SqlConnection connection = new SqlConnection(_connectionString);
            SqlDataReader reader = DBHelper.ExecuteReader(connection, sql);
            while (reader.Read())
            {
                string span = reader["span"].ToString();
                string clock = reader["clock"].ToString();
                string sendpoint = reader["sendpoint"].ToString();
                int count = Convert.ToInt32(reader["count"].ToString());
                SetRecord(span, clock, sendpoint, count);
                
            }
            connection.Close();

            foreach (KeyValuePair<string, int> pair in _dayRecordCount)
            {
                if (pair.Value < 1)
                {
                    Logger.Error(string.Format("丢失排班表数据 {0}", pair.Key));
                }
                else if (pair.Value > 1)
                {
                    Logger.Error(string.Format("排班表数据重复 {0}", pair.Key));
                }
            }

        }
    }
}